﻿/**
 * 借款报销通用utils
 */
 
/**
 * 删除空行
 * 1、如果表体第一行amount为空，则提示
 * 2、如果第一行不为空，删除其余空行 (列表中显示的字段)
 */
function deleteEmptyRow(ds) 
{ 
	if(ds == null || ds.getRowCount() == 0)
		return true;
	var amount = ds.getRow(0).getCellValue(ds.nameToIndex('amount'));
	//姚晡--云南信托，不判断表体合计金额为空
	//if(amount == '' || amount == 0){
	//	showMessageDialog(webbx_trans('yer_je01'));
	//	return false;
	//}   
	var delIndices = new Array();
	for(var k=0,count=ds.getRowCount(); k<count; k++){
		var flag = true; 
		var row = ds.getRow(k);
		var tablecode = row.getCellValue(ds.nameToIndex('tablecode'));
		var grid = getComponent("bodyTab_" + tablecode + "_grid");
		if(grid == null)
			continue; 
		var gIds = grid.getVisibleColumnIds();
		for(var i=0,len=gIds.length; i<len; i++){
			var value = row.getCellValue(ds.nameToIndex(gIds[i]));
			if(value != null && value != '' && value != 0){
				flag = false;
				break;
			}
		}
		if(flag)
			delIndices.push(k);
	}
	if(delIndices.length != 0)
		ds.deleteRows(delIndices);
	return true;
};


/**
 * 校验表头Dataset
 */
function checkDs(headDs){
	if(headDs.id == 'bx_bxzb'){
		var isHk = getParameter("type"); 
		if(isHk != null && isHk == 'hk')
			return true;
		return $chechHeadDs(headDs, ["total","ybje"]);
	}else{
		return $chechHeadDs(headDs, ["ybje"]);
	}
}

function $chechHeadDs(headDs, arr){
	var row = headDs.getSelectedRow();
	for(var i=0,len=arr.length; i<len; i++){
		var index = headDs.nameToIndex(arr[i]);
		var v = row.getCellValue(index); 
		if(v == '' || v == null || v == '0'){
			var msg = headDs.metadata[index].value;
			showMessageDialog(webbx_trans('yer_je02') + msg);
			return false;
		}
	}
	return true;
}

function checkBusItemDs(busitemDs){
	if(busitemDs != null){
		var szxmidIndex = busitemDs.nameToIndex('szxmid');
		var amountIndex = busitemDs.nameToIndex('amount');
		for(var i=0,len=busitemDs.getRowCount();i<len;i++){
			var row = busitemDs.getRow(i);
			var szxmid = row.getCellValue(szxmidIndex);
			var amount = row.getCellValue(amountIndex);
			if(szxmid != null && szxmid != ''){ 
				if($f(amount) == 0 ){ 
					showMessageDialog(webbx_trans('yer_exception01'));
					return false;		
				}
			}    
		}
	}
	//
	return true;
}

/**
 * 重设单据精度
 */
function $resetDjPrecision(headDs,row){
	if(row != null){
		var bbhl_value = row.getCellValue(headDs.nameToIndex('bbhl'));
		var bzbm_value = row.getCellValue(headDs.nameToIndex('bzbm'));
		reset4Bzbm1(headDs,bzbm_value);
		headDs.setValueAt(headDs.getSelectedIndex(),headDs.nameToIndex('bbhl'),bbhl_value);
	}
}

/**
 * 币种变化，则汇率变化,主表的精度、本币金额变化、汇率可写；原币可写
 */
function reset4Bzbm1(headDs,bzbm_value){
	var djlx = (headDs.id == "bx_bxzb") ? "bx" : "jk";
	reset4Bzbm(headDs,bzbm_value,djlx);
}

function reset4Bzbm(headDs,bzbm_value,djlx){
		var jtbwb = getSessionAttribute("jtbwb");
		var bbhlIndex = headDs.nameToIndex('bbhl');
		var form = (djlx == "bx") ? getComponent("headTab_bxzb_form") : getComponent("headTab_jkzb_form");
		var bbhlEle = form.getElement("bbhl");
		if(jtbwb != bzbm_value){
			if(bzbm_value == null)
				return;
			var arr = getService("webbx_common_service").getBbhl(bzbm_value);
			var jdArr = eval(arr);//精度数组：[0]=汇率值、[1]=汇率精度、[2]=币种精度
			headDs.setValueAt(headDs.getSelectedIndex(), bbhlIndex, parseFloat(jdArr[0]));
			resetComponentPrecision(djlx, jdArr);//设置汇率、币种、其他金额的精度
			resetJEForPrecision(djlx); 
			if(bbhlEle != null ){ 
				var status = ButtonManager.getInstance().operStatus;
				if(status == IBillOperate.OP_EDIT || status == IBillOperate.OP_ADD)
					bbhlEle.setActive(true);
			}
			//变更币种后，如果单据有冲借款，则全清空
			if(djlx == "bx"){
				resetCJK4BzbmChange();				
				setPageParamMap('precision',jdArr[2]);
			}
		} 
		else{
			var jt_precision = getSessionAttribute('jtbwb_precision');
			var jdArr = eval(jt_precision);
			//汇率
			headDs.setValueAt(headDs.getSelectedIndex(), bbhlIndex, parseFloat(jdArr[0]));
			resetComponentPrecision(djlx, jdArr)
			resetJEForPrecision(djlx);
			if(bbhlEle != null) 
				bbhlEle.setActive(false);
			if(djlx == "bx"){
				resetCJK4BzbmChange();
				setPageParamMap('precision',jdArr[2]);
			}
		}
}


/*处理摘要,这里要把摘要的名称放入pk字段，因为后台存储的是摘要的名称*/
function dealZyMcToPK(headDs){
	var zyIndex = headDs.nameToIndex('zy');
	var zy_ShowNameIndex = headDs.nameToIndex('zy_mc');
	var zyNameValue = headDs.getSelectedRow().getCellValue(zy_ShowNameIndex);
	if(zyNameValue != null)
		headDs.setValueAt(headDs.getSelectedIndex(),zyIndex,zyNameValue);
}

/*处理摘要，将摘要的名称从pk 属性里取出塞入zy_mc字段*/
function dealZyPKToMc(headDs){
	var row = headDs.getSelectedRow();
	if(row != null){
		var zyIndex = headDs.nameToIndex('zy');
		var zy_ShowNameIndex = headDs.nameToIndex('zy_mc');
		var zyValue = row.getCellValue(zyIndex);
		headDs.setValueAt(headDs.getSelectedIndex(),zy_ShowNameIndex,zyValue);
	}
};

/**
 * 主表汇率变化(1、币种变化引起的；2、直接修改汇率引起的)，则重计算主子表中的本币金额
 * @param 单据类型(not null)、汇率值  
 */
function resetJEforHl(djlx,hl){
	var headDs = null;
	var busDs = null;
	var finDs = null;
	var cDs = null;
	if(djlx == 'bx'){
		headDs = getDataset('bx_bxzb');
		finDs = getDataset('bx_fin');
		cDs = getDataset('bx_contrast');
	}else{
		headDs = getDataset('jk_jkzb');
		cDs = getDataset('jk_bxcontrast');
	}
	if(hl == null){ 
		var hlIndex = headDs.nameToIndex('bbhl');
		hl = parseFloat(headDs.getSelectedRow().getCellValue(hlIndex));
	}
	if(hl == 0)
		return;
	
	var ybArr = ['ybje','ybye','hkybje','zfybje','cjkybje'];
	var bbArr = ['bbje','bbye','hkbbje','zfbbje','cjkbbje'];
	//表头金额(不需要设置了，由财务页签合计来触发)
	if(djlx == "jk"){
		var rowIndex = headDs.getSelectedIndex();
		var row = headDs.getSelectedRow();
		for(var i=0,len=ybArr.length;i<len;i++){
			var ybje = parseFloat(row.getCellValue(headDs.nameToIndex(ybArr[i])));
			if(ybje == 0)
				continue;
			var bbje = ybje*hl;
			headDs.setValueAt(rowIndex,headDs.nameToIndex(bbArr[i]),bbje);
		}
	}else{
		if( finDs != null){
			//财务金额
			for(var k=0,fLen = finDs.getRowCount();k<fLen; k++){
				var fRow = finDs.getRow(k);
				for(var t=0,jeLen=ybArr.length; t<jeLen; t++){
					var ybje = parseFloat(fRow.getCellValue(finDs.nameToIndex(ybArr[t])));
					if(ybje == 0)
						continue;
					var bbje = ybje*hl;
					finDs.setValueAt(k,finDs.nameToIndex(bbArr[t]),bbje);
				}
			}
		}
		if(cDs != null){
			//冲销页面
			/*
			for(var k=0,cLen=cDs.getRowCount(); k<cLen; k++){
				var cRow = cDs.getRow(k);
				var bbje = ybje*hl;
				cDs.setValueAt(k,cDs.nameToIndex('cjkybje'),bbje);
			}
			*/			
		}	
	}
}

/*将finds中的金额字段置零,当币种变化且有重借款时*/
function cleanFinJe(){
	var ybArr = ['ybje','ybye','hkybje','zfybje','cjkybje'];
	var bbArr = ['bbje','bbye','hkbbje','zfbbje','cjkbbje'];
	var finDs = getDataset('bx_fin');
	if(finDs == null)
		return;	
	for(var k=0,fLen = finDs.getRowCount();k<fLen; k++){
		var fRow = finDs.getRow(k);
		for(var t=0,jeLen=ybArr.length; t<jeLen; t++){
			finDs.setValueAt(k,finDs.nameToIndex(ybArr[t]),0);
			finDs.setValueAt(k,finDs.nameToIndex(bbArr[t]),0);
		}
	}
}

/**
 * 主表精度变化(币种变化引起的)，则为页面控件重新设入精度值
 * 设置汇率、其他金额的精度 , 精度数组：[0]=汇率值、[1]=汇率精度、[2]=币种精度
 */
function resetComponentPrecision(djlx,jdArr){
	getSessionAttributeMap()["precision"] = jdArr[2];
	if(djlx == 'bx')
		$resetBxPrecision(jdArr);
	else if(djlx == 'jk')
		$resetJkPrecision(jdArr);
} 

function $resetBxPrecision(jdArr){
	var bbhlEle = getComponent('headTab_bxzb_form').getElement('bbhl');
	if(bbhlEle != null) 
		bbhlEle.setPrecision(jdArr[1]);
	$resetBxPrecision1(jdArr[2]);
}

function $resetBxPrecision1(precision){
	var headFields = ["cjkybje", "cjkbbje", "cjkfbje", 
					  "hkybje", "hkbbje", "hkfbje", 
					  "zfybje", "zfbbje", "zffbje", 
					  "ybje", "bbje", "fbje",
					  "ybye", "bbye", 
					  "total","zpxe"];
	for(var i=0,len = headFields.length; i<len; i++){
		var ele = getComponent('headTab_bxzb_form').getElement(headFields[i]); 
		if(ele != null)
			ele.setPrecision(precision);
	}
	
	var busItemFields = $$getDoubleCol(getDataset("bx_jkbxitems"));
	for(var k=0,len = busItemFields.length; k<len; k++){
		var busGrid = getComponent('bodyTab_arap_bxbusitem_grid');
		if(busGrid != null){
			var ele = busGrid.getHeader(busItemFields[k]);
			if(ele != null)
				ele.setPrecision(precision);
		}
	}
	$$resetFinPrecision(precision);
}

/*获取Ds中所有是double类型的字段*/
function $$getDoubleCol(ds){
	var arr = new Array();
	if(ds != null){
		var meta = ds.metadata;
		for(var m=0,len=meta.length; m<len; m++){
			var key = meta[m].key;
			var datatype = meta[m].dataType;
			if(datatype == DataType.UFDOUBLE || datatype == DataType.DOUBLE || datatype == DataType.dOUBLE || datatype == DataType.Decimal){
				arr.push(key);
			}
		}
	}
	return arr;
}
function $$resetFinPrecision(precision){
	var finItemFields = ["cjkybje", "cjkbbje", "cjkfbje", 
						 "hkybje", "hkbbje", "hkfbje", 
						 "zfybje", "zfbbje", "zffbje", 
						 "ybje", "bbje", "fbje"];
	for(var k=0,len = finItemFields.length; k<len; k++){
		var finGrid = getComponent('bodyTab_er_finitem_grid');
		if(finGrid != null){
			var ele = finGrid.getHeader(finItemFields[k]);
			if(ele != null)
				ele.setPrecision(precision);
		}
	}
}

function $resetJkPrecision(jdArr){
	var bbhlEle = getComponent('headTab_jkzb_form').getElement('bbhl')
	if(bbhlEle != null)
		bbhlEle.setPrecision(jdArr[1]);
	$resetJkPrecision1(jdArr[2]);
}

function $resetJkPrecision1(precision){
	var headFields = ["hkybje", "hkbbje", "hkfbje",
	 				  "zfybje", "zfbbje", "zffbje", 
					  "ybje", "bbje", "fbje",
					  "ybye", "bbye",
					  "total","zpxe"];
	for(var i=0,len = headFields.length; i<len; i++){
		var ele = getComponent('headTab_jkzb_form').getElement(headFields[i]); 
		if(ele != null)
			ele.setPrecision(precision);
	}
	var bodyfields = $$getDoubleCol(getDataset("jk_items"));
	for(var k=0,len = bodyfields.length; k<len;k++){
		var grid = getComponent('bodyTab_arap_bxbusitem_grid');
		if(grid != null){
			var ele = grid.getHeader(bodyfields[k]);
			if(ele != null)
				ele.setPrecision(precision);
		}
	}
}

/**
 * 精度变化，则需要重新setValueAt主子表中的金额 
 */
function resetJEForPrecision(djlx){
	var headDs = null;
	var busDs = null;
	var finDs = null;
	if(djlx == 'bx'){
		headDs = getDataset('bx_bxzb');
		busDs = getDataset('bx_jkbxitems')
		finDs = getDataset('bx_fin');
	}else{
		headDs = getDataset('jk_jkzb');
		busDs = getDataset('jk_items');
	}
	
	/*根据精度重新设置主表的原币金额*/
	$resetHeadYB(headDs);
	try{
		/*重新设置业务页签的amount的精度*/
		$resetBusItem(busDs);
		/*根据精度重新设置财务页签的原币金额，本币变化*/
		$resetFinItemBb(finDs);	
	}catch(error){
		//从列表切换会抛错，故添加
	}
};

function $resetHeadYB(headDs){
	var row = headDs.getSelectedRow();
	if(row == null) 
		return;
	var ybArr = ['ybje','ybye','hkybje','zfybje','cjkybje','total','bbhl'];
	var ybMap = new HashMap();
    for(var i in ybArr)
    	ybMap.put(ybArr[i], ybArr[i]);
    var meta = headDs.metadata;
    var rowIndex = headDs.getSelectedIndex();
	for(var t = 0, count = meta.length; t < count; t++){	
		if(ybMap.containsKey(meta[t].key)){
			var value = row.getCellValue(t);
			headDs.setValueAt(rowIndex, t, value);
		}
	}
};
function $resetBusItem(busDs){
	if(busDs == null) 
		return;
	var busRowcount = busDs.getRowCount();
	var arr = $$getDoubleCol(busDs);
	for(var i=0; i < busRowcount;  i++){
		for(var k=0,len = arr.length; k <len; k++){
			var index = busDs.nameToIndex(arr[k]);
			busDs.setValueAt(i,index,busDs.getRow(i).getCellValue(index));
		}
	}
	
};
function $resetFinItemBb(finDs){
	if(finDs == null)
		return;
	var ybArr = ['ybje','ybye','hkybje','zfybje','cjkybje'];
	var ybMap = new HashMap();
    for(var i in ybArr)
    	ybMap.put(ybArr[i], ybArr[i]);
    var meta = finDs.metadata;
    for(var i=0,count = finDs.getRowCount(); i<count; i++){
    	for(var t=0,mcount = meta.length; t<mcount; t++){
    		if(ybMap.containsKey(meta[t].key)){
    			var row = finDs.getRow(i);
    			var value = row.getCellValue(t);
    			finDs.setValueAt(i,t,value);
    		}
    	}
    }
};

/*将后台返回的xml转换为map数组
 * maps[0]=成功，maps[1]=失败的
*/
function parseXmlToMaps(str){
	var xml = str.toString();
	var dom = createXmlDom(xml);
	var rootNode = dom.documentElement;
	var billList = rootNode.childNodes;

	var successMap = new HashMap();
	var failMap = new HashMap();
	if( billList != null && billList.length > 0){
		for(var i = 0; i < billList.length; i ++){
			var bill = billList[i];
			var pk = bill.getAttribute("pk");
			var node = bill.childNodes;
			var isSuccess = node[0].text;
			var param = isSuccess + "$" + node[1].text + "$" + node[2].text + "$" 
						+ node[3].text + "$" + node[4].text + "$" + node[5].text 
						+ "$" + node[6].text + "$" + node[7].text + "$" + node[8].text;			
			if(isSuccess == 'true')
				successMap.put(pk, param);
			else
				failMap.put(pk, param);
		}
	}
	if(failMap.isEmpty()) 
		failMap = null;
	return [successMap,failMap];
};

/*主表的主键index*/
function getDatasetMasterKeyField(headDs){
	var relArr = getDatasetRelations().getRelationsByMasterDataset(headDs.id);
	var pkIndex = headDs.nameToIndex(relArr[0].masterKeyField);
	return pkIndex;
};
